package semantic

import scala.collection.mutable.ArrayBuffer
import utils.data.CTree
import semantic.rules.CRules
import syntax.CASTNode

class CSemanticAnalyzer(implicit tree:CTree[CASTNode])
{
    private implicit val code = ArrayBuffer[String]()

    def apply():Seq[String] =
    {
        walk(tree root)
        postProcess()
        code
    }

    private def walk(node:CASTNode)
    {
        CRules.WALK_RULES get node.category foreach(_.pre(node))

        node.children foreach (c =>
        {
            CRules.WALK_RULES get node.category foreach(_.preChild(node, c))
            walk(c)
            CRules.WALK_RULES get node.category foreach(_.postChild(node, c))
        })

        CRules.WALK_RULES get node.category foreach(_.post(node))
    }

    private def postProcess() = CRules.POST_PROCESS_RULES foreach (_())
}

object CSemanticAnalyzer
{
    def apply(tree:CTree[CASTNode]) = (new CSemanticAnalyzer()(tree))()
}
